home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / mfb / mfbline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-20  |  21.8 KB  |  976 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: mfbline.c,v 5.10 90/01/23 15:14:37 keith Exp $ */
  25. #include "X.h"
  26.  
  27. #include "gcstruct.h"
  28. #include "windowstr.h"
  29. #include "pixmapstr.h"
  30. #include "regionstr.h"
  31. #include "scrnintstr.h"
  32. #include "mistruct.h"
  33.  
  34. #include "mfb.h"
  35. #include "maskbits.h"
  36.  
  37. /* single-pixel lines on a color frame buffer
  38.  
  39.    NON-SLOPED LINES
  40.    horizontal lines are always drawn left to right; we have to
  41. move the endpoints right by one after they're swapped.
  42.    horizontal lines will be confined to a single band of a
  43. region.  the code finds that band (giving up if the lower
  44. bound of the band is above the line we're drawing); then it
  45. finds the first box in that band that contains part of the
  46. line.  we clip the line to subsequent boxes in that band.
  47.    vertical lines are always drawn top to bottom (y-increasing.)
  48. this requires adding one to the y-coordinate of each endpoint
  49. after swapping.
  50.  
  51.    SLOPED LINES
  52.    when clipping a sloped line, we bring the second point inside
  53. the clipping box, rather than one beyond it, and then add 1 to
  54. the length of the line before drawing it.  this lets us use
  55. the same box for finding the outcodes for both endpoints.  since
  56. the equation for clipping the second endpoint to an edge gives us
  57. 1 beyond the edge, we then have to move the point towards the
  58. first point by one step on the major axis.
  59.    eventually, there will be a diagram here to explain what's going
  60. on.  the method uses Cohen-Sutherland outcodes to determine
  61. outsideness, and a method similar to Pike's layers for doing the
  62. actual clipping.
  63.  
  64. */
  65.  
  66. #define OUTCODES(result, x, y, pbox) \
  67.     if (x < pbox->x1) \
  68.     result |= OUT_LEFT; \
  69.     else if (x >= pbox->x2) \
  70.     result |= OUT_RIGHT; \
  71.     if (y < pbox->y1) \
  72.     result |= OUT_ABOVE; \
  73.     else if (y >= pbox->y2) \
  74.     result |= OUT_BELOW;
  75.  
  76. #define round(dividend, divisor) \
  77. ( (((dividend)<<1) + (divisor)) / ((divisor)<<1) )
  78. #define ceiling(m,n)  (((m)-1)/(n) + 1)
  79.  
  80. /*
  81. #define SignTimes(sign, n) ((sign) * ((int)(n)))
  82. */
  83.  
  84. #define SignTimes(sign, n) \
  85.     ( ((sign)<0) ? -(n) : (n) )
  86.  
  87. #define SWAPINT(i, j) \
  88. {  register int _t = i; \
  89.    i = j; \
  90.    j = _t; \
  91. }
  92.  
  93. #define SWAPPT(i, j) \
  94. {  DDXPointRec _t; \
  95.    _t = i; \
  96.    i = j; \
  97.    j = _t; \
  98. }
  99.    
  100.  
  101. void
  102. #ifdef POLYSEGMENT
  103. mfbSegmentSS (pDrawable, pGC, nseg, pSeg)
  104.     DrawablePtr    pDrawable;
  105.     GCPtr    pGC;
  106.     int        nseg;
  107.     register xSegment    *pSeg;
  108. #else
  109. mfbLineSS (pDrawable, pGC, mode, npt, pptInit)
  110.     DrawablePtr pDrawable;
  111.     GCPtr    pGC;
  112.     int        mode;        /* Origin or Previous */
  113.     int        npt;        /* number of points */
  114.     DDXPointPtr pptInit;
  115. #endif
  116. {
  117.     int nboxInit;
  118.     register int nbox;
  119.     BoxPtr pboxInit;
  120.     register BoxPtr pbox;
  121. #ifndef POLYSEGMENT
  122.     register DDXPointPtr ppt;    /* pointer to list of translated points */
  123. #endif
  124.  
  125.     unsigned int oc1;        /* outcode of point 1 */
  126.     unsigned int oc2;        /* outcode of point 2 */
  127.  
  128.     int *addrl;        /* address of destination pixmap */
  129.     int nlwidth;        /* width in longwords of destination pixmap */
  130.     int xorg, yorg;        /* origin of window */
  131.  
  132.     int adx;        /* abs values of dx and dy */
  133.     int ady;
  134.     int signdx;        /* sign of dx and dy */
  135.     int signdy;
  136.     int e, e1, e2;        /* bresenham error and increments */
  137.     int len;            /* length of segment */
  138.     int axis;            /* major axis */
  139.  
  140.                 /* a bunch of temporaries */
  141.     register int y1, y2;
  142.     register int x1, x2;
  143.     RegionPtr cclip;
  144.     int            alu;
  145.  
  146.     cclip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  147.     alu = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->rop;
  148.     pboxInit = REGION_RECTS(cclip);
  149.     nboxInit = REGION_NUM_RECTS(cclip);
  150.  
  151.     if (pDrawable->type == DRAWABLE_WINDOW)
  152.     {
  153.     addrl = (int *)
  154.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devPrivate.ptr);
  155.     nlwidth = (int)
  156.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devKind) >> 2;
  157.     }
  158.     else
  159.     {
  160.     addrl = (int *)(((PixmapPtr)pDrawable)->devPrivate.ptr);
  161.     nlwidth = (int)(((PixmapPtr)pDrawable)->devKind) >> 2;
  162.     }
  163.  
  164.     xorg = pDrawable->x;
  165.     yorg = pDrawable->y;
  166. #ifdef POLYSEGMENT
  167.     while (nseg--)
  168. #else
  169.     ppt = pptInit;
  170.     x2 = ppt->x + xorg;
  171.     y2 = ppt->y + yorg;
  172.     while(--npt)
  173. #endif
  174.     {
  175.     nbox = nboxInit;
  176.     pbox = pboxInit;
  177.  
  178. #ifdef POLYSEGMENT
  179.     x1 = pSeg->x1 + xorg;
  180.     y1 = pSeg->y1 + yorg;
  181.     x2 = pSeg->x2 + xorg;
  182.     y2 = pSeg->y2 + yorg;
  183.     pSeg++;
  184. #else
  185.     x1 = x2;
  186.     y1 = y2;
  187.     ++ppt;
  188.     if (mode == CoordModePrevious)
  189.     {
  190.         xorg = x1;
  191.         yorg = y1;
  192.     }
  193.     x2 = ppt->x + xorg;
  194.     y2 = ppt->y + yorg;
  195. #endif
  196.  
  197.     if (x1 == x2)
  198.     {
  199.         /* make the line go top to bottom of screen, keeping
  200.            endpoint semantics
  201.         */
  202.         if (y1 > y2)
  203.         {
  204.         register int tmp;
  205.  
  206.         tmp = y2;
  207.         y2 = y1 + 1;
  208.         y1 = tmp + 1;
  209. #ifdef POLYSEGMENT
  210.         if (pGC->capStyle != CapNotLast)
  211.             y1--;
  212. #endif
  213.         }
  214. #ifdef POLYSEGMENT
  215.         else if (pGC->capStyle != CapNotLast)
  216.         y2++;
  217. #endif
  218.         /* get to first band that might contain part of line */
  219.         while ((nbox) && (pbox->y2 <= y1))
  220.         {
  221.         pbox++;
  222.         nbox--;
  223.         }
  224.  
  225.         if (nbox)
  226.         {
  227.         /* stop when lower edge of box is beyond end of line */
  228.         while((nbox) && (y2 >= pbox->y1))
  229.         {
  230.             if ((x1 >= pbox->x1) && (x1 < pbox->x2))
  231.             {
  232.             int y1t, y2t;
  233.             /* this box has part of the line in it */
  234.             y1t = max(y1, pbox->y1);
  235.             y2t = min(y2, pbox->y2);
  236.             if (y1t != y2t)
  237.             {
  238.                 mfbVertS (alu,
  239.                       addrl, nlwidth, 
  240.                       x1, y1t, y2t-y1t);
  241.             }
  242.             }
  243.             nbox--;
  244.             pbox++;
  245.         }
  246.         }
  247. #ifndef POLYSEGMENT
  248.         y2 = ppt->y + yorg;
  249. #endif
  250.     }
  251.     else if (y1 == y2)
  252.     {
  253.         /* force line from left to right, keeping
  254.            endpoint semantics
  255.         */
  256.         if (x1 > x2)
  257.         {
  258.         register int tmp;
  259.  
  260.         tmp = x2;
  261.         x2 = x1 + 1;
  262.         x1 = tmp + 1;
  263. #ifdef POLYSEGMENT
  264.         if (pGC->capStyle != CapNotLast)
  265.             x1--;
  266. #endif
  267.         }
  268. #ifdef POLYSEGMENT
  269.         else if (pGC->capStyle != CapNotLast)
  270.         x2++;
  271. #endif
  272.  
  273.         /* find the correct band */
  274.         while( (nbox) && (pbox->y2 <= y1))
  275.         {
  276.         pbox++;
  277.         nbox--;
  278.         }
  279.  
  280.         /* try to draw the line, if we haven't gone beyond it */
  281.         if ((nbox) && (pbox->y1 <= y1))
  282.         {
  283.         int tmp;
  284.  
  285.         /* when we leave this band, we're done */
  286.         tmp = pbox->y1;
  287.         while((nbox) && (pbox->y1 == tmp))
  288.         {
  289.             int    x1t, x2t;
  290.  
  291.             if (pbox->x2 <= x1)
  292.             {
  293.             /* skip boxes until one might contain start point */
  294.             nbox--;
  295.             pbox++;
  296.             continue;
  297.             }
  298.  
  299.             /* stop if left of box is beyond right of line */
  300.             if (pbox->x1 >= x2)
  301.             {
  302.             nbox = 0;
  303.             break;
  304.             }
  305.  
  306.             x1t = max(x1, pbox->x1);
  307.             x2t = min(x2, pbox->x2);
  308.             if (x1t != x2t)
  309.             {
  310.             mfbHorzS (alu,
  311.                   addrl, nlwidth, 
  312.                   x1t, y1, x2t-x1t);
  313.             }
  314.             nbox--;
  315.             pbox++;
  316.         }
  317.         }
  318. #ifndef POLYSEGMENT
  319.         x2 = ppt->x + xorg;
  320. #endif
  321.     }
  322.     else    /* sloped line */
  323.     {
  324.         adx = x2 - x1;
  325.         ady = y2 - y1;
  326.         signdx = sign(adx);
  327.         signdy = sign(ady);
  328.         adx = abs(adx);
  329.         ady = abs(ady);
  330.  
  331.         if (adx > ady)
  332.         {
  333.         axis = X_AXIS;
  334.         e1 = ady << 1;
  335.         e2 = e1 - (adx << 1);
  336.         e = e1 - adx;
  337.  
  338.         }
  339.         else
  340.         {
  341.         axis = Y_AXIS;
  342.         e1 = adx << 1;
  343.         e2 = e1 - (ady << 1);
  344.         e = e1 - ady;
  345.         }
  346.  
  347.         /* we have bresenham parameters and two points.
  348.            all we have to do now is clip and draw.
  349.         */
  350.  
  351.         while(nbox--)
  352.         {
  353.         oc1 = 0;
  354.         oc2 = 0;
  355.         OUTCODES(oc1, x1, y1, pbox);
  356.         OUTCODES(oc2, x2, y2, pbox);
  357.         if ((oc1 | oc2) == 0)
  358.         {
  359.             if (axis == X_AXIS)
  360.             len = adx;
  361.             else
  362.             len = ady;
  363. #ifdef POLYSEGMENT
  364.             if (pGC->capStyle != CapNotLast)
  365.             len++;
  366. #endif
  367.             mfbBresS (alu,
  368.               addrl, nlwidth,
  369.               signdx, signdy, axis, x1, y1,
  370.               e, e1, e2, len);
  371.             break;
  372.         }
  373.         else if (oc1 & oc2)
  374.         {
  375.             pbox++;
  376.         }
  377.         else
  378.         {
  379.                 /*
  380.                   * let the mfb helper routine do our work;
  381.                   * better than duplicating code...
  382.                   */
  383.                 BoxRec box;
  384.                     DDXPointRec pt1Copy;    /* clipped start point */
  385.                     DDXPointRec pt2Copy;    /* clipped end point */
  386.                     int err;            /* modified bresenham error term */
  387.                     int clip1, clip2;        /* clippedness of the endpoints */
  388.                 
  389.                     int clipdx, clipdy;        /* difference between clipped and
  390.                                          unclipped start point */
  391.             DDXPointRec    pt1;
  392.                 
  393.         
  394.                 pt1.x = pt1Copy.x = x1;
  395.             pt1.y = pt1Copy.y = y1;
  396.                 pt2Copy.x = x2;
  397.             pt2Copy.y = y2;
  398.                 box.x1 = pbox->x1;
  399.                 box.y1 = pbox->y1;
  400.                 box.x2 = pbox->x2-1;
  401.                 box.y2 = pbox->y2-1;
  402.                 clip1 = 0;
  403.                 clip2 = 0;
  404.         
  405.             if (mfbClipLine (pbox, box,
  406.                      &pt1, &pt1Copy, &pt2Copy, 
  407.                      adx, ady, signdx, signdy, axis,
  408.                      &clip1, &clip2) == 1)
  409.             {
  410.                 if (axis == X_AXIS)
  411.                 len = abs(pt2Copy.x - pt1Copy.x);
  412.                 else
  413.                 len = abs(pt2Copy.y - pt1Copy.y);
  414.     
  415. #ifdef POLYSEGMENT
  416.                 if (clip2 != 0 || pGC->capStyle != CapNotLast)
  417.                 len++;
  418. #else
  419.                 len += (clip2 != 0);
  420. #endif
  421.                 if (len)
  422.                 {
  423.                 /* unwind bresenham error term to first point */
  424.                 if (clip1)
  425.                 {
  426.                     clipdx = abs(pt1Copy.x - x1);
  427.                     clipdy = abs(pt1Copy.y - y1);
  428.                     if (axis == X_AXIS)
  429.                     err = e+((clipdy*e2) + ((clipdx-clipdy)*e1));
  430.                     else
  431.                     err = e+((clipdx*e2) + ((clipdy-clipdx)*e1));
  432.                 }
  433.                 else
  434.                     err = e;
  435.                 mfbBresS   
  436.                      (alu,
  437.                       addrl, nlwidth,
  438.                       signdx, signdy, axis, pt1Copy.x, pt1Copy.y,
  439.                       err, e1, e2, len);
  440.                 }
  441.             }
  442.             pbox++;
  443.         }
  444.         } /* while (nbox--) */
  445.     } /* sloped line */
  446.     } /* while (nline--) */
  447.  
  448. #ifndef POLYSEGMENT
  449.  
  450.     /* paint the last point if the end style isn't CapNotLast.
  451.        (Assume that a projecting, butt, or round cap that is one
  452.         pixel wide is the same as the single pixel of the endpoint.)
  453.     */
  454.  
  455.     if ((pGC->capStyle != CapNotLast) &&
  456.     ((ppt->x != pptInit->x) ||
  457.      (ppt->y != pptInit->y) ||
  458.      (ppt == pptInit + 1)))
  459.     {
  460.     unsigned int _mask;
  461.     int _incr;
  462.  
  463.     if (alu == RROP_BLACK)
  464.         _mask = rmask[x2 & 0x1f];
  465.     else
  466.         _mask = mask[x2 & 0x1f];
  467.     _incr = (y2 * nlwidth) + (x2 >> 5);
  468.  
  469.     nbox = nboxInit;
  470.     pbox = pboxInit;
  471.     while (nbox--)
  472.     {
  473.         if ((x2 >= pbox->x1) &&
  474.         (y2 >= pbox->y1) &&
  475.         (x2 <  pbox->x2) &&
  476.         (y2 <  pbox->y2))
  477.         {
  478.         addrl += _incr;
  479.         switch(alu)
  480.         {
  481.             case RROP_BLACK:
  482.                 *addrl &= _mask;
  483.             break;
  484.             case RROP_WHITE:
  485.                 *addrl |= _mask;
  486.             break;
  487.             case RROP_INVERT:
  488.                 *addrl ^= _mask;
  489.             break;
  490.         }
  491.         break;
  492.         }
  493.         else
  494.         pbox++;
  495.     }
  496.     }
  497. #endif
  498. }
  499.  
  500. /*
  501.  * Draw dashed 1-pixel lines.
  502.  */
  503.  
  504. void
  505. #ifdef POLYSEGMENT
  506. mfbSegmentSD (pDrawable, pGC, nseg, pSeg)
  507.     DrawablePtr    pDrawable;
  508.     register GCPtr    pGC;
  509.     int        nseg;
  510.     register xSegment    *pSeg;
  511. #else
  512. mfbLineSD( pDrawable, pGC, mode, npt, pptInit)
  513.     DrawablePtr pDrawable;
  514.     register GCPtr pGC;
  515.     int mode;        /* Origin or Previous */
  516.     int npt;        /* number of points */
  517.     DDXPointPtr pptInit;
  518. #endif
  519. {
  520.     int nboxInit;
  521.     register int nbox;
  522.     BoxPtr pboxInit;
  523.     register BoxPtr pbox;
  524. #ifndef POLYSEGMENT
  525.     register DDXPointPtr ppt;    /* pointer to list of translated points */
  526. #endif
  527.  
  528.     register unsigned int oc1;    /* outcode of point 1 */
  529.     register unsigned int oc2;    /* outcode of point 2 */
  530.  
  531.     int *addrl;        /* address of destination pixmap */
  532.     int nlwidth;        /* width in longwords of destination pixmap */
  533.     int xorg, yorg;        /* origin of window */
  534.  
  535.     int adx;        /* abs values of dx and dy */
  536.     int ady;
  537.     int signdx;        /* sign of dx and dy */
  538.     int signdy;
  539.     int e, e1, e2;        /* bresenham error and increments */
  540.     int len;            /* length of segment */
  541.     int axis;            /* major axis */
  542.     int x1, x2, y1, y2;
  543.     RegionPtr cclip;
  544.     int            fgrop, bgrop;
  545.     unsigned char   *pDash;
  546.     int            dashOffset;
  547.     int            numInDashList;
  548.     int            dashIndex;
  549.     int            isDoubleDash;
  550.     int            dashIndexTmp, dashOffsetTmp;
  551.     int            unclippedlen;
  552.  
  553.     cclip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  554.     fgrop = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->rop;
  555.     pboxInit = REGION_RECTS(cclip);
  556.     nboxInit = REGION_NUM_RECTS(cclip);
  557.  
  558.     if (pDrawable->type == DRAWABLE_WINDOW)
  559.     {
  560.     addrl = (int *)
  561.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devPrivate.ptr);
  562.     nlwidth = (int)
  563.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devKind) >> 2;
  564.     }
  565.     else
  566.     {
  567.     addrl = (int *)(((PixmapPtr)pDrawable)->devPrivate.ptr);
  568.     nlwidth = (int)(((PixmapPtr)pDrawable)->devKind) >> 2;
  569.     }
  570.  
  571.     /* compute initial dash values */
  572.      
  573.     pDash = (unsigned char *) pGC->dash;
  574.     numInDashList = pGC->numInDashList;
  575.     isDoubleDash = (pGC->lineStyle == LineDoubleDash);
  576.     dashIndex = 0;
  577.     dashOffset = 0;
  578.     miStepDash ((int)pGC->dashOffset, &dashIndex, pDash,
  579.         numInDashList, &dashOffset);
  580.  
  581.     if (isDoubleDash)
  582.     bgrop = mfbReduceRop(pGC->alu, pGC->bgPixel);
  583.  
  584.     xorg = pDrawable->x;
  585.     yorg = pDrawable->y;
  586. #ifdef POLYSEGMENT
  587.     while (nseg--)
  588. #else
  589.     ppt = pptInit;
  590.     x2 = ppt->x + xorg;
  591.     y2 = ppt->y + yorg;
  592.     while(--npt)
  593. #endif
  594.     {
  595.     nbox = nboxInit;
  596.     pbox = pboxInit;
  597.  
  598. #ifdef POLYSEGMENT
  599.     x1 = pSeg->x1 + xorg;
  600.     y1 = pSeg->y1 + yorg;
  601.     x2 = pSeg->x2 + xorg;
  602.     y2 = pSeg->y2 + yorg;
  603.     pSeg++;
  604. #else
  605.     x1 = x2;
  606.     y1 = y2;
  607.     ++ppt;
  608.     if (mode == CoordModePrevious)
  609.     {
  610.         xorg = x1;
  611.         yorg = y1;
  612.     }
  613.     x2 = ppt->x + xorg;
  614.     y2 = ppt->y + yorg;
  615. #endif
  616.  
  617.     adx = x2 - x1;
  618.     ady = y2 - y1;
  619.     signdx = sign(adx);
  620.     signdy = sign(ady);
  621.     adx = abs(adx);
  622.     ady = abs(ady);
  623.  
  624.     if (adx > ady)
  625.     {
  626.         axis = X_AXIS;
  627.         e1 = ady << 1;
  628.         e2 = e1 - (adx << 1);
  629.         e = e1 - adx;
  630.         unclippedlen = adx;
  631.     }
  632.     else
  633.     {
  634.         axis = Y_AXIS;
  635.         e1 = adx << 1;
  636.         e2 = e1 - (ady << 1);
  637.         e = e1 - ady;
  638.         unclippedlen = ady;
  639.     }
  640.  
  641.     /* we have bresenham parameters and two points.
  642.        all we have to do now is clip and draw.
  643.     */
  644.  
  645.     while(nbox--)
  646.     {
  647.         oc1 = 0;
  648.         oc2 = 0;
  649.         OUTCODES(oc1, x1, y1, pbox);
  650.         OUTCODES(oc2, x2, y2, pbox);
  651.         if ((oc1 | oc2) == 0)
  652.         {
  653. #ifdef POLYSEGMENT
  654.         if (pGC->capStyle != CapNotLast)
  655.             unclippedlen++;
  656.         dashIndexTmp = dashIndex;
  657.         dashOffsetTmp = dashOffset;
  658.         mfbBresD (fgrop, bgrop,
  659.               &dashIndexTmp, pDash, numInDashList,
  660.               &dashOffsetTmp, isDoubleDash,
  661.               addrl, nlwidth,
  662.               signdx, signdy, axis, x1, y1,
  663.               e, e1, e2, unclippedlen);
  664.         break;
  665. #else
  666.         mfbBresD (fgrop, bgrop,
  667.               &dashIndex, pDash, numInDashList,
  668.               &dashOffset, isDoubleDash,
  669.               addrl, nlwidth,
  670.               signdx, signdy, axis, x1, y1,
  671.               e, e1, e2, unclippedlen);
  672.         goto dontStep;
  673. #endif
  674.         }
  675.         else if (oc1 & oc2)
  676.         {
  677.         pbox++;
  678.         }
  679.         else /* have to clip */
  680.         {
  681.         /*
  682.          * let the mfb helper routine do our work;
  683.          * better than duplicating code...
  684.          */
  685.         BoxRec box;
  686.         DDXPointRec pt1Copy;    /* clipped start point */
  687.         DDXPointRec pt2Copy;    /* clipped end point */
  688.         int err;            /* modified bresenham error term */
  689.         int clip1, clip2;        /* clippedness of the endpoints */
  690.         
  691.         int clipdx, clipdy;        /* difference between clipped and
  692.                            unclipped start point */
  693.         DDXPointRec    pt1;
  694.     
  695.         pt1.x = pt1Copy.x = x1;
  696.         pt1.y = pt1Copy.y = y1;
  697.         pt2Copy.x = x2;
  698.         pt2Copy.y = y2;
  699.         box.x1 = pbox->x1;
  700.         box.y1 = pbox->y1;
  701.         box.x2 = pbox->x2-1;
  702.         box.y2 = pbox->y2-1;
  703.         clip1 = 0;
  704.         clip2 = 0;
  705.     
  706.         if (mfbClipLine (pbox, box,
  707.                        &pt1, &pt1Copy, &pt2Copy, 
  708.                        adx, ady, signdx, signdy, axis,
  709.                        &clip1, &clip2) == 1)
  710.         {
  711.     
  712.             dashIndexTmp = dashIndex;
  713.             dashOffsetTmp = dashOffset;
  714.             if (clip1)
  715.             {
  716.                 int dlen;
  717.     
  718.                 if (axis == X_AXIS)
  719.                 dlen = abs(pt1Copy.x - x1);
  720.                 else
  721.                 dlen = abs(pt1Copy.y - y1);
  722.                 miStepDash (dlen, &dashIndexTmp, pDash,
  723.                     numInDashList, &dashOffsetTmp);
  724.             }
  725.             if (axis == X_AXIS)
  726.                 len = abs(pt2Copy.x - pt1Copy.x);
  727.             else
  728.                 len = abs(pt2Copy.y - pt1Copy.y);
  729.     
  730. #ifdef POLYSEGMENT
  731.             if (clip2 != 0 || pGC->capStyle != CapNotLast)
  732.                 len++;
  733. #else
  734.             len += (clip2 != 0);
  735. #endif
  736.             if (len)
  737.             {
  738.                 /* unwind bresenham error term to first point */
  739.                 if (clip1)
  740.                 {
  741.                 clipdx = abs(pt1Copy.x - x1);
  742.                 clipdy = abs(pt1Copy.y - y1);
  743.                 if (axis == X_AXIS)
  744.                     err = e+((clipdy*e2) + ((clipdx-clipdy)*e1));
  745.                 else
  746.                     err = e+((clipdx*e2) + ((clipdy-clipdx)*e1));
  747.                 }
  748.                 else
  749.                 err = e;
  750.                 mfbBresD (fgrop, bgrop,
  751.                         &dashIndexTmp, pDash, numInDashList,
  752.                         &dashOffsetTmp, isDoubleDash,
  753.                         addrl, nlwidth,
  754.                         signdx, signdy, axis, pt1Copy.x, pt1Copy.y,
  755.                         err, e1, e2, len);
  756.             }
  757.         }
  758.         pbox++;
  759.         }
  760.     } /* while (nbox--) */
  761. #ifndef POLYSEGMENT
  762.     /*
  763.      * walk the dash list around to the next line
  764.      */
  765.     miStepDash (unclippedlen, &dashIndex, pDash,
  766.             numInDashList, &dashOffset);
  767. dontStep:    ;
  768. #endif
  769.     } /* while (nline--) */
  770.  
  771. #ifndef POLYSEGMENT
  772.     /* paint the last point if the end style isn't CapNotLast.
  773.        (Assume that a projecting, butt, or round cap that is one
  774.         pixel wide is the same as the single pixel of the endpoint.)
  775.     */
  776.  
  777.     if ((pGC->capStyle != CapNotLast) &&
  778.         ((dashIndex & 1) == 0 || isDoubleDash) &&
  779.     ((ppt->x != pptInit->x) ||
  780.      (ppt->y != pptInit->y) ||
  781.      (ppt == pptInit + 1)))
  782.     {
  783.     nbox = nboxInit;
  784.     pbox = pboxInit;
  785.     while (nbox--)
  786.     {
  787.         if ((x2 >= pbox->x1) &&
  788.         (y2 >= pbox->y1) &&
  789.         (x2 <  pbox->x2) &&
  790.         (y2 <  pbox->y2))
  791.         {
  792.         unsigned long _mask;
  793.         int rop;
  794.  
  795.         rop = fgrop;
  796.         if (dashIndex & 1)
  797.             rop = bgrop;
  798.         if (rop == RROP_BLACK)
  799.             _mask = rmask[x2 & 0x1f];
  800.         else
  801.             _mask = mask[x2 & 0x1f];
  802.         addrl += (y2 * nlwidth) + (x2 >> 5);
  803.         if (rop == RROP_BLACK)
  804.             *addrl &= _mask;
  805.         else if (rop == RROP_WHITE)
  806.             *addrl |= _mask;
  807.         else
  808.             *addrl ^= _mask;
  809.         break;
  810.         }
  811.         else
  812.         pbox++;
  813.     }
  814.     }
  815. #endif
  816. }
  817.  
  818. #ifndef POLYSEGMENT
  819. /*
  820.     the clipping code could be cleaned up some; most of its
  821. mess derives from originally being inline in the line code,
  822. then pulled out to make clipping dashes easier.
  823. */
  824.  
  825. int
  826. mfbClipLine(pbox, box,
  827.         ppt1Orig, ppt1, ppt2, 
  828.         adx, ady, signdx, signdy, axis,
  829.         pclip1, pclip2)
  830. BoxPtr pbox;            /* box to clip to */
  831. BoxRec box;            /* box to do calculations with */
  832. DDXPointPtr ppt1Orig, ppt1, ppt2;
  833. int adx, ady;
  834. int signdx, signdy;
  835. register int axis;
  836. int *pclip1, *pclip2;
  837. {
  838.     DDXPointRec pt1Orig, pt1, pt2;
  839.     register int swapped = 0;
  840.     int clipDone = 0;
  841.     register unsigned long utmp;
  842.     register int oc1, oc2;
  843.     int clip1, clip2;
  844.  
  845.     pt1Orig = *ppt1Orig;
  846.     pt1 = *ppt1;
  847.     pt2 = *ppt2;
  848.     clip1 = 0;
  849.     clip2 = 0;
  850.  
  851.     do
  852.     {
  853.         oc1 = 0;
  854.         oc2 = 0;
  855.         OUTCODES(oc1, pt1.x, pt1.y, pbox);
  856.         OUTCODES(oc2, pt2.x, pt2.y, pbox);
  857.  
  858.         if (oc1 & oc2)
  859.         clipDone = -1;
  860.         else if ((oc1 | oc2) == 0)
  861.         {
  862.         clipDone = 1;
  863.         if (swapped)
  864.         {
  865.             SWAPPT(pt1, pt2);
  866.             SWAPINT(oc1, oc2);
  867.             SWAPINT(clip1, clip2);
  868.         }
  869.         }
  870.         else /* have to clip */
  871.         {
  872.         /* only clip one point at a time */
  873.         if (!oc1)
  874.         {
  875.             SWAPPT(pt1, pt2);
  876.             SWAPINT(oc1, oc2);
  877.             SWAPINT(clip1, clip2);
  878.             swapped = !swapped;
  879.         }
  880.     
  881.         clip1 |= oc1;
  882.         if (oc1 & OUT_LEFT)
  883.         {
  884.           pt1.x = box.x1;
  885.           utmp = abs(box.x1 - pt1Orig.x);
  886.           utmp *= ady;
  887.           if(axis==X_AXIS)
  888.           {
  889.             pt1.y = pt1Orig.y + SignTimes(signdy, round(utmp, adx));
  890.           }
  891.           else
  892.           {
  893.         utmp <<= 1;
  894.         if (swapped)
  895.             utmp += ady;
  896.         else
  897.             utmp -= ady;
  898.         pt1.y = pt1Orig.y + SignTimes(signdy, ceiling(utmp, 2*adx));
  899.         if (swapped)
  900.             pt1.y -= signdy;
  901.           }
  902.         }
  903.         else if (oc1 & OUT_ABOVE)
  904.         {
  905.           pt1.y = box.y1;
  906.           utmp = abs(box.y1 - pt1Orig.y);
  907.           utmp *= adx;
  908.           if (axis == Y_AXIS)
  909.           {
  910.             pt1.x = pt1Orig.x + SignTimes(signdx, round(utmp, ady));
  911.           }
  912.           else
  913.           {
  914.         utmp <<= 1;
  915.         if (swapped)
  916.             utmp += adx;
  917.         else
  918.             utmp -= adx;
  919.         pt1.x = pt1Orig.x + SignTimes(signdx, ceiling(utmp, 2*ady));
  920.         if (swapped)
  921.             pt1.x -= signdx;
  922.           }
  923.         }
  924.         else if (oc1 & OUT_RIGHT)
  925.         {
  926.           pt1.x = box.x2;
  927.           utmp = abs(pt1Orig.x - box.x2);
  928.           utmp *= ady;
  929.           if (axis == X_AXIS)
  930.           {
  931.             pt1.y = pt1Orig.y + SignTimes(signdy, round(utmp, adx));
  932.           }
  933.           else
  934.           {
  935.         utmp <<= 1;
  936.         if (swapped)
  937.             utmp += ady;
  938.         else
  939.             utmp -= ady;
  940.         pt1.y = pt1Orig.y + SignTimes(signdy, ceiling(utmp, 2*adx));
  941.         if (swapped)
  942.             pt1.y -= signdy;
  943.           }
  944.         }
  945.         else if (oc1 & OUT_BELOW)
  946.         {
  947.           pt1.y = box.y2;
  948.           utmp = abs(pt1Orig.y - box.y2);
  949.           utmp *= adx;
  950.           if (axis == Y_AXIS)
  951.           {
  952.             pt1.x = pt1Orig.x + SignTimes(signdx, round(utmp, ady));
  953.           }
  954.           else
  955.           {
  956.         utmp <<= 1;
  957.         if (swapped)
  958.             utmp += adx;
  959.         else
  960.             utmp -= adx;
  961.         pt1.x = pt1Orig.x + SignTimes(signdx, ceiling(utmp, 2*ady));
  962.         if (swapped)
  963.             pt1.x -= signdx;
  964.           }
  965.         }
  966.         } /* else have to clip */
  967.     } while(!clipDone);
  968.     *ppt1 = pt1;
  969.     *ppt2 = pt2;
  970.     *pclip1 = clip1;
  971.     *pclip2 = clip2;
  972.  
  973.     return clipDone;
  974. }
  975. #endif
  976.